<template>
  <div class="app-container">
    <div>
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="所属项目" required>
          <el-select
            v-model="form.belong_project"
            filterable
            placeholder="请选择项目">
            <el-option
              v-for="item in search.project_options"
              :key="item.id"
              :label="item.project_name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="导入文件" required>
          <div style="width:30%">
            <el-upload
              class="upload-demo"
              ref="uploadBox"
              accept=".json,.JSON"
              :on-remove="batchRemoveFile"
              :on-change="batchImportChange"
              :on-exceed="batchImportExceed"
              :file-list="fileList"
              :limit="10"
              drag
              action="action"
              :auto-upload="false"
              multiple>
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            </el-upload>
          </div>
      </el-form-item>
      </el-form>
    </div>
    <br>
    <div style="text-align: center">
      <el-button type="success" @click="loadFile">上传</el-button>
      <el-button @click="onCancel">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getProjectSumList } from '@/api/project/table'
import { loadJSON } from '@/api/load'

export default {
  data() {
    return {
      fileList: [],
      form: {
        belong_project: '',
        file: ''
      },
      search: {
        project_options: [],
        filter_project_id: null
      },
      list: null
    }
  },
  created() {
    this.fetchProjectData()
  },

  methods: {
    fetchProjectData() {
      getProjectSumList().then(response => {
        this.search.project_options = response.results
        console.log(this.search.project_options)
      })
    },

    // 批量导入-文件状态改变时的钩子
    batchImportChange(file, fileList) {
      this.fileList = fileList
    },

    // 文件列表移除文件时的钩子
    batchRemoveFile(file, fileList) {
      this.fileList = fileList
    },

    // 批量导入-定义超出限制时的行为
    batchImportExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 10 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      )
    },

    loadFile() {
      var formData = new FormData()
      this.fileList.forEach(file => {
        formData.append('file', file.raw)
      })
      formData.append('belong_project', this.form.belong_project)
      formData.append('type', 'config')
      loadJSON(formData).then(response => {
        if (response.status === '000000') return
        this.$message.success('文件上传成功')
        this.$router.replace(`/config/config_list`)
      })
    },

    onCancel() {
      this.$confirm('此操作将退出编辑, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.replace(`/config/config_list`)
        this.$message({
          type: 'success',
          message: '退出成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        })
      })
    }
  }
}
</script>

<style scoped lang="scss">
.line {
  text-align: center;
}

.input-reader-name {
  width: 40%;
}

.flex-box {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-content: center;

  .inline-box {
    display: inline-block;
    width: 350px;

    &:last-child {
      width: 500px;
    }
  }

  > span {
    font-size: 30px;
    line-height: 290px;
  }
}

</style>
